from typing import Optional

import pygame


def draw_cursor(surface, cur_position, high, c, size=4):
    pygame.draw.line(surface, c,
                     cur_position,
                     (cur_position[0], cur_position[1] + high), size)


def render_text(surface, pos, font: pygame.font, text: str, c=pygame.Color('black'),
                cursor_position: Optional[int] = None):
    """Render text with hyphenation"""

    def get_word_length(ft, word: str, c):
        word_surface = ft.render(word, 0, c)
        return word_surface.get_size()
    if len(text) == 0:
        return None

    need_cursor = cursor_position is not None
    lines = [word.split() for word in text.splitlines()]
    space_size = font.size(' ')[0]
    max_width, max_height = surface.get_size()

    cursor = x, y = pos
    symbols_counter = 0
    for line in lines:
        for word in line:
            size = w, h = get_word_length(font, word, c)
            if w > max_width or h > max_height:
                raise ValueError("Surface is too small")
            if x + w >= max_width:
                x = pos[0]
                y += h
            surface.blit(font.render(word, 0, c), (x, y))
            if need_cursor and symbols_counter + len(word) >= cursor_position:
                # It means that cursor is in this word or after this word
                cur_coord = (x + get_word_length(font,
                                                 word[:cursor_position - symbols_counter], c)[0], y)
                draw_cursor(surface, cur_coord, h, c)
                need_cursor = False
            symbols_counter += len(word) + 1
            x += w + space_size
        y += h
        x = pos[0]